// ----------------------------------------------------------------------------------
// Microsoft Developer & Platform Evangelism
// 
// Copyright (c) Microsoft Corporation. All rights reserved.
// 
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
// ----------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
// ----------------------------------------------------------------------------------

namespace Riolinx.SocialConnekt.Library.Configuration
{
    using System;
    using System.Configuration;
    using Riolinx.SocialConnekt.Library.Model;
    using Riolinx.SocialConnekt.Library.Utilities;

    public class DefaultConfigurationRepository : IConfigurationRepository
    {
        public ClaimProvider RetrieveIssuer(Uri identifier)
        {
            var configuration = ConfigurationManager.GetSection("riolinx.identityModel/socialConnekt") as MultiProtocolIssuerSection;
            var claimProvider = configuration.ClaimProviders[identifier.ToString()];

            var issuer = claimProvider.ToModel();
            return issuer;
        }

        public MultiProtocolIssuer RetrieveMultiProtocolIssuer()
        {
            var configuration = ConfigurationManager.GetSection("riolinx.identityModel/socialConnekt") as MultiProtocolIssuerSection;

            return new MultiProtocolIssuer
            {
                Identifier = new Uri(configuration.Identifier),
                ReplyUrl = new Uri(configuration.ResponseEndpoint),
                SigningCertificate =
                    CertificateUtil.GetCertificate(
                        configuration.SigningCertificate.StoreName,
                        configuration.SigningCertificate.StoreLocation,
                        configuration.SigningCertificate.FindValue)
            };
        }

        public Scope RetrieveScopeById(Uri identifier)
        {
            var configuration = ConfigurationManager.GetSection("riolinx.identityModel/socialConnekt") as MultiProtocolIssuerSection;

            var scope = configuration.Scopes[identifier.ToString()];

            if (scope == null && identifier.ToString().EndsWith("/"))
            {
                scope = configuration.Scopes[identifier.ToString().Substring(0, identifier.ToString().Length - 1)];
            }
            var model = scope.ToModel();

            return model;
        }

        public Scope RetrieveScopeByUri(Uri uri)
        {
            var configuration = ConfigurationManager.GetSection("riolinx.identityModel/socialConnekt") as MultiProtocolIssuerSection;

            var scopes = configuration.Scopes;

            ScopeElement selectedScope = null;

            foreach (ScopeElement scope in scopes)
            {
                if (scope.Uri == uri.ToString())
                {
                    selectedScope = scope;
                    break;
                }
            }

            
            return selectedScope.ToModel();
        }
    }
}